package definite.Guide.Book.BoundedRangeComponents;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.BoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class JScrollBarSample {

	static class BoundChangeListener implements ChangeListener {

		@Override
		public void stateChanged(ChangeEvent e) {
			Object source = e.getSource();
			if (source instanceof BoundedRangeModel) {
				BoundedRangeModel aModel = (BoundedRangeModel) source;
				if (!aModel.getValueIsAdjusting())
					System.out.println("Changed: " + aModel.getValue());
				else
					System.out.println("SomeThing Changed : " + source);
			}
		}

	}

	public static void main(String[] args) {
		Runnable r = new Runnable() {

			@Override
			public void run() {
				AdjustmentListener adjustmentListener =
					new AdjustmentListener() {

						@Override
						public void adjustmentValueChanged(AdjustmentEvent e) {
							System.out.println("Adjusted : " + e.getValue());
						}
					};

				JScrollBar oneBar = new JScrollBar(JScrollBar.HORIZONTAL);
				oneBar.addAdjustmentListener(adjustmentListener);

				JScrollBar anotherBar = new JScrollBar(JScrollBar.HORIZONTAL);
				ChangeListener changedListener = new BoundChangeListener();
				BoundedRangeModel model = anotherBar.getModel();
				model.addChangeListener(changedListener);

				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("JScrollBar Sample");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.add(oneBar, BorderLayout.NORTH);
				frame.add(anotherBar, BorderLayout.SOUTH);
				frame.setLocationRelativeTo(null);
				frame.setSize(300, 200);
				frame.setVisible(true);
			}

		};

		EventQueue.invokeLater(r);
	}

}
